package com.billdowney.qq.robot.plugins.provide;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.RandomUtil;
import com.billdowney.qq.robot.bean.ConfigBean;
import com.billdowney.qq.robot.plugins.RobotPlugin;
import com.billdowney.qq.robot.plugins.RobotPluginContent;
import com.billdowney.qq.robot.plugins.RobotPluginInfo;
import com.billdowney.qq.robot.util.MessageUtil;
import kotlin.io.FilesKt;
import net.mamoe.mirai.contact.Contact;
import net.mamoe.mirai.contact.Group;
import net.mamoe.mirai.utils.BotConfiguration;
import net.mamoe.mirai.utils.RemoteFile;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;

/**
 * 媒体插件
 *
 * @author BillDowney
 * @date 2021/4/9 9:45
 */
@Component
public class MediaRobotPlugin implements RobotPlugin, InitializingBean {

    @Autowired
    private ConfigBean configBean;
    /**
     * 图片文件夹
     */
    private File imgFile;
    /**
     * 斗图文件夹
     */
    private File stickerFile;
    /**
     * 短视频文件夹
     */
    private File shortVideoFile;

    @Override
    public void afterPropertiesSet() throws Exception {
        // 媒体文件夹
        File baseMediaPath = FilesKt.resolve(BotConfiguration.getDefault().getWorkingDir(), configBean.getWorkspace() + File.separator + "media" + File.separator);
        this.imgFile = FilesKt.resolve(baseMediaPath, "img");
        this.stickerFile = FilesKt.resolve(baseMediaPath, "sticker");
        this.shortVideoFile = FilesKt.resolve(baseMediaPath, "short_video");
        FileUtil.mkdir(this.imgFile);
        FileUtil.mkdir(this.stickerFile);
        FileUtil.mkdir(this.shortVideoFile);
    }

    @Override
    public RobotPluginInfo info() {
        return RobotPluginInfo.create("媒体插件", false)
                .addDescLn("使用方式：")
                .addDescLn("#看图")
                .addDescLn("#斗图")
                .addDescLn("#短视频(只支持qq群)");
    }

    @Override
    public void execute(RobotPluginContent content) {
        if (content.getRobotEventEnum().isMessageEvent()) {
            switch (content.getContent()) {
                case "#看图":
                    File[] imgList = imgFile.listFiles();
                    if (imgList == null || imgList.length == 0) {
                        content.putReplyMessage("没有图可以看哦~");
                    } else {
                        Contact contact = null;
                        if (content.getRobotEventEnum().isGroupMsg()) {
                            contact = content.getGroupMessageEvent().getGroup();
                        } else {
                            contact = content.getFriendMessageEvent().getSender();
                        }
                        content.putReplyMessage(contact, MessageUtil.buildImageMessage(contact, RandomUtil.randomEle(imgList)));
                    }
                    break;
                case "#斗图":
                    File[] stickerList = stickerFile.listFiles();
                    if (stickerList == null || stickerList.length == 0) {
                        content.putReplyMessage("没有表情包可以看哦~");
                    } else {
                        Contact contact = null;
                        if (content.getRobotEventEnum().isGroupMsg()) {
                            contact = content.getGroupMessageEvent().getGroup();
                        } else {
                            contact = content.getFriendMessageEvent().getSender();
                        }
                        content.putReplyMessage(contact, MessageUtil.buildImageMessage(contact, RandomUtil.randomEle(stickerList)));
                    }
                    break;
                case "#短视频":
                    if (content.getRobotEventEnum().isGroupMsg()) {
                        File[] shortVideoList = shortVideoFile.listFiles();
                        if (shortVideoList == null || shortVideoList.length == 0) {
                            content.putReplyMessage("没有短视频可以看哦~");
                        } else {
                            // 暂时有bug
                            Group group = content.getGroupMessageEvent().getGroup();
                            RemoteFile remoteFile = group.getFilesRoot();
                            File sendFile = RandomUtil.randomEle(shortVideoList);
                            content.putReplyMessage(group, remoteFile.resolve(sendFile.getName()).upload(sendFile));
                        }
                    }
                    break;
                default:
                    break;
            }
        }
    }

}
